package com.yanze.schoolinformation.viewModule.circle;

import android.graphics.BitmapFactory;
import android.net.Uri;

import com.yanze.schoolinformation.app.C;
import com.yanze.schoolinformation.model.bean.req.ReqDynamics;
import com.yanze.schoolinformation.model.bean.req.ReqSecondBook;
import com.yanze.schoolinformation.model.bean.resp.RespCurr;
import com.yanze.schoolinformation.model.bean.resp.RespImage;
import com.yanze.schoolinformation.model.http.DeforObserver;
import com.yanze.schoolinformation.model.http.reqo.DataReqo;
import com.yanze.schoolinformation.utils.FilePathManager;
import com.yanze.schoolinformation.utils.ToolUtils;
import com.yanze.schoolinformation.utils.UpFile;
import com.yanze.schoolinformation.view.community.circle.PublishDynamicActivity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by MrZheng on 2018/5/9.
 */

public class PublishDynamicViewModel {
    private PublishDynamicActivity mActivity;
    private PublishDynamicNavigator mNavigator;
    private List<Uri> waitImages;//待上传图片
    private List<String> successImages = new ArrayList<>();//上传成功图片

    private int mCurrindex = 0;

    public PublishDynamicViewModel(PublishDynamicActivity mActivity) {
        this.mActivity = mActivity;
    }

    public void setmNavigator(PublishDynamicNavigator mNavigator) {
        this.mNavigator = mNavigator;
    }

    /**
     * 发表动态
     */
    public void publicDynamic(ReqDynamics dynamics) {
        DataReqo.getInstant().publishDynamic(C.url.API_School,dynamics)
                .subscribe(new DeforObserver<RespCurr>() {
                    @Override
                    public void _onSubscribe(Disposable disposable) {
                        mActivity.addSubscription(disposable);
                    }

                    @Override
                    public void _onNext(RespCurr entity) {
                        if (entity.getStatus() == 200) {
                            mNavigator.publishSuccess();
                        }else{
                            mNavigator.publishFaild(entity.getMsg());
                        }
                    }

                    @Override
                    public void _onError(String msg) {
                        mNavigator.publishFaild(msg);
                    }
                });
    }

    /**
     * 发表二手书信息
     */
    public void publishSecondBook(ReqSecondBook secondBook) {
        DataReqo.getInstant().publishBook(C.url.API_School, secondBook)
                .subscribe(new DeforObserver<RespCurr>() {
                    @Override
                    public void _onSubscribe(Disposable disposable) {
                        mActivity.addSubscription(disposable);
                    }

                    @Override
                    public void _onNext(RespCurr entity) {
                        if (entity.getStatus() == 200) {
                            mNavigator.publishSuccess();
                        }else{
                            mNavigator.publishFaild(entity.getMsg());
                        }
                    }

                    @Override
                    public void _onError(String msg) {
                        mNavigator.publishFaild(msg);
                    }
                });
    }

    /**
     * 上传图片
     * @param waitImages
     */
    public void upImage(List<Uri> waitImages) {
        successImages.clear();
        if (waitImages == null || waitImages.isEmpty()) {
            return;
        }
        this.waitImages = waitImages;
        this.mCurrindex = 0;
        up();
    }

    public void resert() {
        up();
    }

    /**
     * 请求网络
     */
    private void up() {
        mNavigator.onProgrees(mCurrindex + 1);
        String realPath = ToolUtils.getRealPathFromUriAboveApi19(mActivity, waitImages.get(mCurrindex));

        //压缩图片
        ToolUtils.compressBitmap(BitmapFactory.decodeFile(realPath), FilePathManager.getImageFilePath(mActivity), 500);
        File file = FilePathManager.getImageFilePath(mActivity);

        MediaType IMAGE_TYPE = MediaType.parse("image/jpeg");
        RequestBody requestBody = RequestBody.create(IMAGE_TYPE, file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
        DataReqo.getInstant().upImage(C.url.API_School, body)
                .subscribe(new DeforObserver<RespImage>() {
                    @Override
                    public void _onSubscribe(Disposable disposable) {
                        mActivity.addSubscription(disposable);
                    }

                    @Override
                    public void _onNext(RespImage entity) {
                        if (entity.getStatus() == 200) {
                            successImages.add(entity.getData());
                            mCurrindex++;
                            if (mCurrindex < waitImages.size()) {
                                up();
                            }else{
                                mNavigator.onSucceed(successImages);
                            }
                        } else {
                            mNavigator.onFailed(mCurrindex + 1,entity.getMsg());
                        }
                    }

                    @Override
                    public void _onError(String msg) {
                        mNavigator.onFailed(mCurrindex + 1,msg);
                    }
                });
    }

    public void onDestory() {
        this.mNavigator = null;
        this.mActivity = null;
    }
}
